import axios from 'axios'
import { Message,Loading,MessageBox } from 'element-ui';
import { hostUrl} from '../../config/request.config'
import config from './config'
import { parse } from 'url';


let Token,codeFlag = true;
let isLoginning =false;

export const request = (option) =>{
    let loadingInstance1,
        loginFalse = window.pathName==='login'?false:true; // 判断是否联营登录
    if(codeFlag){
        if(!option.hideLoading){
            loadingInstance1 = Loading.service({ fullscreen: true });
        }
        let _config = Object.assign(config,option)
        _config.baseURL = "";
        if(option.host){
            _config.baseURL = option.host;
        }else{
            _config.baseURL = hostUrl;
        }
        _config.method = option.method ? option.method : 'post';
        if(_config.method ==='get'){
            _config.params = _config.data
        }
    
        return new Promise((resolve, reject)=>{
            if(!option.hideLoading){
                loadingInstance1.close();
            }
            resolve(axios(_config))
        }).then(res=>{
            let _res = res.data,
                msg = '',
                msgbtn = '',
                msgConfig = {};
            _res = typeof _res === 'string' ? parse(_res) : _res
            if(_res.code ==0 || _res.msgCode==100){
                return _res
            }else if(_res.code === -6 || _res.code === -7){
                let _configCall = {};
                if(_res.code == -6&&!isLoginning){
                    msg = '您的账号未登录，请重新登录哦';
                    msgbtn='去登录';

                    if(loginFalse){// 非联营厅登录
                        isLoginning = true;
                        _configCall = {callback: action => {
                            
                            let uInfo = localStorage.getItem('MEN-DIAN-UINFO');
                            if(uInfo!=undefined && uInfo!=''){
                                localStorage.setItem("MEN-DIAN-UINFO","");
                            }
                            window.location.href=_res.msg;
                        }}
                    }
                    msgConfig = Object.assign({confirmButtonText: msgbtn},_configCall)
                    if(isLoginning){
                        MessageBox.alert(msg,'提示',msgConfig)
                    }
                }else if(_res.code == -7&&!isLoginning){
                    msg = '当前模块您暂无权限哦';
                    msgbtn='知道了';
                    if(loginFalse){// 非联营厅登录
                        isLoginning = true;
                        _configCall = {callback: action => {
                            
                            //这个接口没权限，且不存在相关配置，那么页面后退
                            if(!noBackApis || !noBackApis.has(option.url)) {
                                history.go(-1);
                            }
                        }}
                    }
                    msgConfig = Object.assign({confirmButtonText: msgbtn},_configCall)
                    if(isLoginning){
                        MessageBox.alert(msg,'提示',msgConfig)
                    }
                }
                
                
            }else{
                if(!option.hideMsg){
                    Message({
                        showClose: true,
                        message: _res.msg ? _res.msg : _res.message,
                        type: 'error'
                    });
                }else{
                    return _res;
                }
            }

        }).catch(error =>{
            // Message({
            //     showClose: true,
            //     message: error,
            //     type: 'error'
            // });
        })
    }
}